home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / make-367.lha / make-3.67 / main.c < prev    next >
C/C++ Source or Header  |  1993-05-22  |  45KB  |  1,724 lines

  1. /* Copyright (C) 1988, 1989, 1990, 1991 Free Software Foundation, Inc.
  2. This file is part of GNU Make.
  3.  
  4. GNU Make is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8.  
  9. GNU Make is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with GNU Make; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include "make.h"
  19. #include "commands.h"
  20. #include "dep.h"
  21. #include "file.h"
  22. #include "variable.h"
  23. #include "job.h"
  24. #include "getopt.h"
  25.  
  26.  
  27. extern char *version_string;
  28.  
  29. extern struct dep *read_all_makefiles ();
  30.  
  31. extern void print_variable_data_base ();
  32. extern void print_dir_data_base ();
  33. extern void print_rule_data_base ();
  34. extern void print_file_data_base ();
  35. extern void print_vpath_data_base ();
  36.  
  37. #ifndef    HAVE_UNISTD_H
  38. extern int chdir ();
  39. #endif
  40. #ifndef    STDC_HEADERS
  41. #ifndef    sun            /* Sun has an incorrect decl in a header.  */
  42. extern void exit ();
  43. #endif
  44. extern double atof ();
  45. #endif
  46. extern char *mktemp ();
  47.  
  48. static void log_working_directory ();
  49. static void print_data_base (), print_version ();
  50. static void decode_switches (), decode_env_switches ();
  51. static void define_makeflags ();
  52.  
  53.  
  54. #if 0 /* dummy tag */
  55. flags () {}
  56. #endif
  57. /* Flags:
  58.  *    -b ignored for compatibility with System V Make
  59.  *    -C change directory
  60.  *    -d debug
  61.  *    -e env_overrides
  62.  *    -f makefile
  63.  *    -i ignore_errors
  64.  *    -j job_slots
  65.  *    -k keep_going
  66.  *    -l max_load_average
  67.  *    -m ignored for compatibility with something or other
  68.  *    -n just_print
  69.  *    -o consider file old
  70.  *    -p print_data_base
  71.  *    -q question
  72.  *    -r no_builtin_rules
  73.  *    -s silent
  74.  *    -S turn off -k
  75.  *    -t touch
  76.  *    -v print version information
  77.  *    -w log working directory
  78.  *    -W consider file new (with -n, `what' if effect)
  79.  */
  80.  
  81. /* The structure that describes an accepted command switch.  */
  82.  
  83. struct command_switch
  84.   {
  85.     char c;            /* The switch character.  */
  86.  
  87.     enum            /* Type of the value.  */
  88.       {
  89.     flag,            /* Turn int flag on.  */
  90.     flag_off,        /* Turn int flag off.  */
  91.     string,            /* One string per switch.  */
  92.     positive_int,        /* A positive integer.  */
  93.     floating,        /* A floating-point number (double).  */
  94.     ignore            /* Ignored.  */
  95.       } type;
  96.  
  97.     char *value_ptr;    /* Pointer to the value-holding variable.  */
  98.  
  99.     unsigned int env:1;        /* Can come from MAKEFLAGS.  */
  100.     unsigned int toenv:1;    /* Should be put in MAKEFLAGS.  */
  101.     unsigned int no_makefile:1;    /* Don't propagate when remaking makefiles.  */
  102.  
  103.     char *noarg_value;    /* Pointer to value used if no argument is given.  */
  104.     char *default_value;/* Pointer to default value.  */
  105.  
  106.     char *long_name;        /* Long option name.  */
  107.     char *argdesc;        /* Descriptive word for argument.  */
  108.     char *description;        /* Description for usage message.  */
  109.   };
  110.  
  111.  
  112. /* The structure used to hold the list of strings given
  113.    in command switches of a type that takes string arguments.  */
  114.  
  115. struct stringlist
  116.   {
  117.     char **list;    /* Nil-terminated list of strings.  */
  118.     unsigned int idx;    /* Index into above.  */
  119.     unsigned int max;    /* Number of pointers allocated.  */
  120.   };
  121.  
  122.  
  123. /* The recognized command switches.  */
  124.  
  125. /* Nonzero means do not print commands to be executed (-s).  */
  126.  
  127. int silent_flag;
  128.  
  129. /* Nonzero means just touch the files
  130.    that would appear to need remaking (-t)  */
  131.  
  132. int touch_flag;
  133.  
  134. /* Nonzero means just print what commands would need to be executed,
  135.    don't actually execute them (-n).  */
  136.  
  137. int just_print_flag;
  138.  
  139. /* Print debugging trace info (-d).  */
  140.  
  141. int debug_flag = 0;
  142.  
  143. /* Environment variables override makefile definitions.  */
  144.  
  145. int env_overrides = 0;
  146.  
  147. /* Nonzero means ignore status codes returned by commands
  148.    executed to remake files.  Just treat them all as successful (-i).  */
  149.  
  150. int ignore_errors_flag = 0;
  151.  
  152. /* Nonzero means don't remake anything, just print the data base
  153.    that results from reading the makefile (-p).  */
  154.  
  155. int print_data_base_flag = 0;
  156.  
  157. /* Nonzero means don't remake anything; just return a nonzero status
  158.    if the specified targets are not up to date (-q).  */
  159.  
  160. int question_flag = 0;
  161.  
  162. /* Nonzero means do not use any of the builtin rules (-r).  */
  163.  
  164. int no_builtin_rules_flag = 0;
  165.  
  166. /* Nonzero means keep going even if remaking some file fails (-k).  */
  167.  
  168. int keep_going_flag;
  169. int default_keep_going_flag = 0;
  170.  
  171. /* Nonzero means print directory before starting and when done (-w).  */
  172.  
  173. int print_directory_flag = 0;
  174.  
  175. /* Nonzero means ignore print_directory_flag and never print the directory.
  176.    This is necessary because print_directory_flag is set implicitly.  */
  177.  
  178. int inhibit_print_directory_flag = 0;
  179.  
  180. /* Nonzero means print version information.  */
  181.  
  182. int print_version_flag = 0;
  183.  
  184. /* List of makefiles given with -f switches.  */
  185.  
  186. static struct stringlist *makefiles = 0;
  187.  
  188.  
  189. /* Number of job slots (commands that can be run at once).  */
  190.  
  191. unsigned int job_slots = 1;
  192. unsigned int default_job_slots = 1;
  193.  
  194. /* Value of job_slots that means no limit.  */
  195.  
  196. static unsigned int inf_jobs = 0;
  197.  
  198. /* Maximum load average at which multiple jobs will be run.
  199.    Negative values mean unlimited, while zero means limit to
  200.    zero load (which could be useful to start infinite jobs remotely
  201.    but one at a time locally).  */
  202.  
  203. double max_load_average = -1.0;
  204. double default_load_average = -1.0;
  205.  
  206. /* List of directories given with -c switches.  */
  207.  
  208. static struct stringlist *directories = 0;
  209.  
  210. /* List of include directories given with -I switches.  */
  211.  
  212. static struct stringlist *include_directories = 0;
  213.  
  214. /* List of files given with -o switches.  */
  215.  
  216. static struct stringlist *old_files = 0;
  217.  
  218. /* List of files given with -W switches.  */
  219.  
  220. static struct stringlist *new_files = 0;
  221.  
  222. /* If nonzero, we should just print usage and exit.  */
  223.  
  224. static int print_usage_flag = 0;
  225.  
  226. /* The table of command switches.  */
  227.  
  228. static const struct command_switch switches[] =
  229.   {
  230.     { 'b', ignore, 0, 0, 0, 0, 0, 0,
  231.     0, 0,
  232.     "Ignored for compatibility" },
  233.     { 'C', string, (char *) &directories, 0, 0, 0, 0, 0,
  234.     "directory", "DIRECTORY",
  235.     "Change to DIRECTORY before doing anything" },
  236.     { 'd', flag, (char *) &debug_flag, 1, 1, 0, 0, 0,
  237.     "debug", 0,
  238.     "Print lots of debugging information" },
  239.     { 'e', flag, (char *) &env_overrides, 1, 1, 0, 0, 0,
  240.     "environment-overrides", 0,
  241.     "Environment variables override makefiles" },
  242.     { 'f', string, (char *) &makefiles, 0, 0, 0, 0, 0,
  243.     "file", "FILE",
  244.     "Read FILE as a makefile" },
  245.     { 'h', flag, (char *) &print_usage_flag, 0, 0, 0, 0, 0,
  246.     "help", 0,
  247.     "Print this message and exit" },
  248.     { 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0, 0,
  249.     "ignore-errors", 0,
  250.     "Ignore errors from commands" },
  251.     { 'I', string, (char *) &include_directories, 1, 1, 0, 0, 0,
  252.     "include-dir", "DIRECTORY",
  253.     "Search DIRECTORY for included makefiles" },
  254.     { 'j', positive_int, (char *) &job_slots, 1, 1, 0,
  255.     (char *) &inf_jobs, (char *) &default_job_slots,
  256.     "jobs", "N",
  257.     "Allow N jobs at once; infinite jobs with no arg" },
  258.     { 'k', flag, (char *) &keep_going_flag, 1, 1, 0,
  259.     0, (char *) &default_keep_going_flag,
  260.     "keep-going", 0,
  261.     "Keep going when some targets can't be made" },
  262.     { 'l', floating, (char *) &max_load_average, 1, 1, 0,
  263.     (char *) &default_load_average, (char *) &default_load_average,
  264.     "load-average", "N",
  265.     "Don't start multiple jobs unless load is below N" },
  266.     { 'm', ignore, 0, 0, 0, 0, 0, 0,
  267.     0, 0,
  268.     "-b" },
  269.     { 'n', flag, (char *) &just_print_flag, 1, 1, 1, 0, 0,
  270.     "just-print", 0,
  271.     "Don't actually run any commands; just print them" },
  272.     { 'o', string, (char *) &old_files, 0, 0, 0, 0, 0,
  273.     "old-file", "FILE",
  274.     "Consider FILE to be very old and don't remake it" },
  275.     { 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0, 0,
  276.     "print-data-base", 0,
  277.     "Print make's internal database" },
  278.     { 'q', flag, (char *) &question_flag, 1, 1, 1, 0, 0,
  279.     "question", 0,
  280.     "Run no commands; exit status says if up to date" },
  281.     { 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0, 0,
  282.     "no-builtin-rules", 0,
  283.     "Disable the built-in implicit rules" },
  284.     { 's', flag, (char *) &silent_flag, 1, 1, 0, 0, 0,
  285.     "silent", 0,
  286.     "Don't echo commands" },
  287.     { 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0,
  288.     0, (char *) &default_keep_going_flag,
  289.     "no-keep-going", 0,
  290.     "Turns off -k" },
  291.     { 't', flag, (char *) &touch_flag, 1, 1, 1, 0, 0,
  292.     "touch", 0,
  293.     "Touch targets instead of remaking them" },
  294.     { 'v', flag, (char *) &print_version_flag, 1, 1, 0, 0, 0,
  295.     "version", 0,
  296.     "Print the version number of make and exit" },
  297.     { 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0, 0,
  298.     "print-directory", 0,
  299.     "Print the current directory" },
  300.     { 1, flag, (char *) &inhibit_print_directory_flag, 1, 1, 0, 0, 0,
  301.     "no-print-directory", 0,
  302.     "Turn off -w, even if it was turned on implicitly" },
  303.     { 'W', string, (char *) &new_files, 0, 0, 0, 0, 0,
  304.     "what-if", "FILE",
  305.     "Consider FILE to be infinitely new" },
  306.     { '\0', }
  307.   };
  308.  
  309. /* Secondary long names for options.  */
  310.  
  311. static struct option long_option_aliases[] =
  312.   {
  313.     { "quiet",        no_argument,        0, 's' },
  314.     { "stop",        no_argument,        0, 'S' },
  315.     { "new-file",    required_argument,    0, 'W' },
  316.     { "assume-new",    required_argument,    0, 'W' },
  317.     { "assume-old",    required_argument,    0, 'o' },
  318.     { "max-load",    optional_argument,    0, 'l' },
  319.     { "dry-run",    no_argument,        0, 'n' },
  320.     { "recon",        no_argument,        0, 'n' },
  321.     { "makefile",    required_argument,    0, 'f' },
  322.   };
  323.  
  324. /* The usage message prints the descriptions of options starting in
  325.    this column.  Make sure it leaves enough room for the longest
  326.    description to fit in less than 80 characters.  */
  327.  
  328. #define    DESCRIPTION_COLUMN    30
  329.  
  330. /* List of non-switch arguments.  */
  331.  
  332. struct stringlist *other_args = 0;
  333.  
  334. /* The name we were invoked with.  */
  335.  
  336. char *program;
  337.  
  338. /* Our current directory after processing all -C options.  */
  339.  
  340. char *starting_directory;
  341.  
  342. /* Value of the MAKELEVEL variable at startup (or 0).  */
  343.  
  344. unsigned int makelevel;
  345.  
  346. /* First file defined in the makefile whose name does not
  347.    start with `.'.  This is the default to remake if the
  348.    command line does not specify.  */
  349.  
  350. struct file *default_goal_file;
  351.  
  352. /* Pointer to structure for the file .DEFAULT
  353.    whose commands are used for any file that has none of its own.
  354.    This is zero if the makefiles do not define .DEFAULT.  */
  355.  
  356. struct file *default_file;
  357.  
  358. /* Mask of signals that are being caught with fatal_error_signal.  */
  359.  
  360. #ifdef    POSIX
  361. sigset_t fatal_signal_set;
  362. #else
  363. #ifdef    HAVE_SIGSETMASK
  364. int fatal_signal_mask;
  365. #endif
  366. #endif
  367.  
  368. int
  369. main (argc, argv, envp)
  370.      int argc;
  371.      char **argv;
  372.      char **envp;
  373. {
  374.   extern void init_dir ();
  375.   extern RETSIGTYPE fatal_error_signal (), child_handler ();
  376.   register struct file *f;
  377.   register unsigned int i;
  378.   register char *cmd_defs;
  379.   register unsigned int cmd_defs_len, cmd_defs_idx;
  380.   char **p;
  381.   time_t now;
  382.   struct dep *goals = 0;
  383.   register struct dep *lastgoal;
  384.   struct dep *read_makefiles;
  385.   PATH_VAR (current_directory);
  386.   char *directory_before_chdir;
  387.  
  388.   default_goal_file = 0;
  389.   reading_filename = 0;
  390.   reading_lineno_ptr = 0;
  391.   
  392. #ifndef    HAVE_SYS_SIGLIST
  393.   signame_init ();
  394. #endif
  395.  
  396. #ifdef    POSIX
  397.   sigemptyset (&fatal_signal_set);
  398. #define    ADD_SIG(sig)    sigaddset (&fatal_signal_set, sig)
  399. #else
  400. #ifdef    HAVE_SIGSETMASK
  401.   fatal_signal_mask = 0;
  402. #define    ADD_SIG(sig)    fatal_signal_mask |= sigmask (sig)
  403. #else
  404. #define    ADD_SIG(sig)
  405. #endif
  406. #endif
  407.  
  408. #define    FATAL_SIG(sig)                                  \
  409.   if (signal ((sig), fatal_error_signal) == SIG_IGN)                  \
  410.     (void) signal ((sig), SIG_IGN);                          \
  411.   else                                          \
  412.     ADD_SIG (sig);
  413.  
  414.   FATAL_SIG (SIGHUP);
  415.   FATAL_SIG (SIGQUIT);
  416.   FATAL_SIG (SIGINT);
  417.   FATAL_SIG (SIGTERM);
  418.  
  419. #ifdef    SIGDANGER
  420.   FATAL_SIG (SIGDANGER);
  421. #endif
  422. #ifdef SIGXCPU
  423.   FATAL_SIG (SIGXCPU);
  424. #endif
  425. #ifdef SIGXFSZ
  426.   FATAL_SIG (SIGXFSZ);
  427. #endif
  428.  
  429. #undef    FATAL_SIG
  430.  
  431.   /* Make sure stdout is line-buffered.  */
  432.  
  433. #ifdef    HAVE_SETLINEBUF
  434.   setlinebuf (stdout);
  435. #else
  436. #ifndef    SETVBUF_REVERSED
  437.   setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
  438. #else    /* setvbuf not reversed.  */
  439.   /* Some buggy systems lose if we pass 0 instead of allocating ourselves.  */
  440.   setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
  441. #endif    /* setvbuf reversed.  */
  442. #endif    /* setlinebuf missing.  */
  443.  
  444.   /* Initialize the directory hashing code.  */
  445.   init_dir ();
  446.  
  447.   /* Set up to access user data (files).  */
  448.   user_access ();
  449.  
  450.   /* Figure out where this program lives.  */
  451.  
  452.   if (argv[0] == 0)
  453.     argv[0] = "";
  454.   if (argv[0][0] == '\0')
  455.     program = "make";
  456.   else 
  457.     {
  458.       program = rindex (argv[0], '/');
  459.       if (program == 0)
  460.     program = argv[0];
  461.       else
  462.     ++program;
  463.     }
  464.  
  465.   /* Figure out where we are.  */
  466.  
  467.   if (getcwd (current_directory, GET_PATH_MAX) == 0)
  468.     {
  469. #ifdef    HAVE_GETCWD
  470.       perror_with_name ("getcwd: ", "");
  471. #else
  472.       error ("getwd: %s", current_directory);
  473. #endif
  474.       current_directory[0] = '\0';
  475.       directory_before_chdir = 0;
  476.     }
  477.   else
  478.     directory_before_chdir = savestring (current_directory,
  479.                      strlen (current_directory));
  480.  
  481.   /* Read in variables from the environment.  It is important that this be
  482.      done before `MAKE' and `MAKEOVERRIDES' are figured out so their
  483.      definitions will not be ones from the environment.  */
  484.  
  485.   for (i = 0; envp[i] != 0; ++i)
  486.     {
  487.       register char *ep = envp[i];
  488.       while (*ep++ != '=')
  489.     ;
  490.       (void) define_variable (envp[i], ep - envp[i] - 1, ep, o_env, 1);
  491.     }
  492.  
  493.   /* Decode the switches.  */
  494.  
  495.   decode_env_switches ("MAKEFLAGS", 9);
  496.   decode_env_switches ("MFLAGS", 6);
  497.   decode_switches (argc, argv, 0);
  498.  
  499.   /* Print version information.  */
  500.  
  501.   if (print_version_flag || print_data_base_flag || debug_flag)
  502.     print_version ();
  503.  
  504.   /* `make --version' is supposed to just print the version and exit.  */
  505.   if (print_version_flag)
  506.     die (0);
  507.  
  508.   /* Search for command line arguments that define variables,
  509.      and do the definitions.  Also save up the text of these
  510.      arguments in CMD_DEFS so we can put them into the values
  511.      of $(MAKEOVERRIDES) and $(MAKE).  */
  512.  
  513.   cmd_defs_len = 200;
  514.   cmd_defs = (char *) xmalloc (cmd_defs_len);
  515.   cmd_defs_idx = 0;
  516.  
  517.   for (i = 1; other_args->list[i] != 0; ++i)
  518.     {
  519.       if (other_args->list[i][0] == '\0')
  520.     /* Ignore empty arguments, so they can't kill enter_file.  */
  521.     continue;
  522.  
  523.       /* Try a variable definition.  */
  524.       if (try_variable_definition ((char *) 0, 0,
  525.                    other_args->list[i], o_command))
  526.     {
  527.       /* It succeeded.  The variable is already defined.
  528.          Backslash-quotify it and append it to CMD_DEFS, then clobber it
  529.          to 0 in the list so that it won't be taken for a goal target.  */
  530.       register char *p = other_args->list[i];
  531.       unsigned int l = strlen (p);
  532.       if (cmd_defs_idx + (l * 2) + 1 > cmd_defs_len)
  533.         {
  534.           if (l * 2 > cmd_defs_len)
  535.         cmd_defs_len += l * 2;
  536.           else
  537.         cmd_defs_len *= 2;
  538.           cmd_defs = (char *) xrealloc (cmd_defs, cmd_defs_len);
  539.         }
  540.       
  541.       while (*p != '\0')
  542.         {
  543.           if (index (";'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *p) != 0)
  544.         cmd_defs[cmd_defs_idx++] = '\\';
  545.           cmd_defs[cmd_defs_idx++] = *p++;
  546.         }
  547.       cmd_defs[cmd_defs_idx++] = ' ';
  548.     }
  549.       else
  550.     {
  551.       /* It was not a variable definition, so it is a target to be made.
  552.          Enter it as a file and add it to the dep chain of goals.  */
  553.       f = enter_file (other_args->list[i]);
  554.       f->cmd_target = 1;
  555.       
  556.       if (goals == 0)
  557.         {
  558.           goals = (struct dep *) xmalloc (sizeof (struct dep));
  559.           lastgoal = goals;
  560.         }
  561.       else
  562.         {
  563.           lastgoal->next = (struct dep *) xmalloc (sizeof (struct dep));
  564.           lastgoal = lastgoal->next;
  565.         }
  566.       lastgoal->name = 0;
  567.       lastgoal->file = f;
  568.     }
  569.     }
  570.  
  571.   if (cmd_defs_idx > 0)
  572.     {
  573.       cmd_defs[cmd_defs_idx - 1] = '\0';
  574.       (void) define_variable ("MAKEOVERRIDES", 13, cmd_defs, o_default, 0);
  575.     }
  576.   free (cmd_defs);
  577.  
  578.   /* Set the "MAKE_COMMAND" variable to the name we were invoked with.
  579.      (If it is a relative pathname with a slash, prepend our directory name
  580.      so the result will run the same program regardless of the current dir.
  581.      If it is a name with no slash, we can only hope that PATH did not
  582.      find it in the current directory.)  */
  583.  
  584.   if (current_directory[0] != '\0'
  585.       && argv[0] != 0 && argv[0][0] != '/' && index (argv[0], '/') != 0)
  586.     argv[0] = concat (current_directory, "/", argv[0]);
  587.  
  588.   (void) define_variable ("MAKE_COMMAND", 12, argv[0], o_default, 0);
  589.  
  590.   /* Append the command-line variable definitions gathered above
  591.      so sub-makes will get them as command-line definitions.  */
  592.  
  593.   (void) define_variable ("MAKE", 4,
  594.               "$(MAKE_COMMAND) $(MAKEOVERRIDES)", o_default, 1);
  595.  
  596.   /* If there were -C flags, move ourselves about.  */
  597.  
  598.   if (directories != 0)
  599.     for (i = 0; directories->list[i] != 0; ++i)
  600.       {
  601.     char *dir = directories->list[i];
  602.     if (chdir (dir) < 0)
  603.       pfatal_with_name (dir);
  604.       }
  605.  
  606.   /* Figure out the level of recursion.  */
  607.   {
  608.     struct variable *v = lookup_variable ("MAKELEVEL", 9);
  609.     if (v != 0 && *v->value != '\0' && *v->value != '-')
  610.       makelevel = (unsigned int) atoi (v->value);
  611.     else
  612.       makelevel = 0;
  613.   }
  614.  
  615.   /* Except under -s, always do -w in sub-makes and under -C.  */
  616.   if (!silent_flag && (directories != 0 || makelevel > 0))
  617.     print_directory_flag = 1;
  618.  
  619.   /* Let the user disable that with --no-print-directory.  */
  620.   if (inhibit_print_directory_flag)
  621.     print_directory_flag = 0;
  622.  
  623.   /* Construct the list of include directories to search.  */
  624.  
  625.   construct_include_path (include_directories == 0 ? (char **) 0
  626.               : include_directories->list);
  627.  
  628.   /* Figure out where we are now, after chdir'ing.  */
  629.   if (directories == 0)
  630.     /* We didn't move, so we're still in the same place.  */
  631.     starting_directory = current_directory;
  632.   else
  633.     {
  634.       if (getcwd (current_directory, GET_PATH_MAX) == 0)
  635.     {
  636. #ifdef    HAVE_GETCWD
  637.       perror_with_name ("getcwd: ", "");
  638. #else
  639.       error ("getwd: %s", current_directory);
  640. #endif
  641.       starting_directory = 0;
  642.     }
  643.       else
  644.     starting_directory = current_directory;
  645.     }
  646.  
  647.   /* Tell the user where he is.  */
  648.  
  649.   if (print_directory_flag)
  650.     log_working_directory (1);
  651.  
  652.   /* Read any stdin makefiles into temporary files.  */
  653.  
  654.   if (makefiles != 0)
  655.     {
  656.       register unsigned int i;
  657.       for (i = 0; i < makefiles->idx; ++i)
  658.     if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
  659.       {
  660.         /* This makefile is standard input.  Since we may re-exec
  661.            and thus re-read the makefiles, we read standard input
  662.            into a temporary file and read from that.  */
  663.         static char name[] = "/tmp/GmXXXXXX";
  664.         FILE *outfile;
  665.  
  666.         /* Free the storage allocated for "-".  */
  667.         free (makefiles->list[i]);
  668.  
  669.         /* Make a unique filename.  */
  670.         (void) mktemp (name);
  671.  
  672.         outfile = fopen (name, "w");
  673.         if (outfile == 0)
  674.           pfatal_with_name ("fopen (temporary file)");
  675.         while (!feof (stdin))
  676.           {
  677.         char buf[2048];
  678.         int n = fread (buf, 1, sizeof(buf), stdin);
  679.         if (n > 0 && fwrite (buf, 1, n, outfile) != n)
  680.           pfatal_with_name ("fwrite (temporary file)");
  681.           }
  682.         /* Try to make sure we won't remake the temporary
  683.            file when we are re-exec'd.  Kludge-o-matic!  */
  684.         fprintf (outfile, "%s:;\n", name);
  685.         (void) fclose (outfile);
  686.  
  687.         /* Replace the name that read_all_makefiles will
  688.            see with the name of the temporary file.  */
  689.         makefiles->list[i] = savestring (name, sizeof name - 1);
  690.  
  691.         /* Make sure the temporary file will not be remade.  */
  692.         f = enter_file (savestring (name, sizeof name - 1));
  693.         f->updated = 1;
  694.         f->update_status = 0;
  695.         f->command_state = cs_finished;
  696.         /* Let it be removed when we're done.  */
  697.         f->intermediate = 1;
  698.         /* But don't mention it.  */
  699.         f->dontcare = 1;
  700.       }
  701.     }
  702.  
  703.   /* Set up to handle children dying.  This must be done before
  704.      reading in the makefiles so that `shell' function calls will work.  */
  705.  
  706. #ifdef SIGCHLD
  707.   (void) signal (SIGCHLD, child_handler);
  708. #endif
  709. #ifdef SIGCLD
  710.   (void) signal (SIGCLD, child_handler);
  711. #endif
  712.  
  713.   /* Define the initial list of suffixes for old-style rules.  */
  714.  
  715.   set_default_suffixes ();
  716.  
  717.   /* Define some internal and special variables.  */
  718.  
  719.   define_automatic_variables ();
  720.  
  721.   /* Set up the MAKEFLAGS and MFLAGS variables
  722.      so makefiles can look at them.  */
  723.  
  724.   define_makeflags (0, 0);
  725.  
  726.   /* Define the default variables.  */
  727.   define_default_variables ();
  728.  
  729.   /* Read all the makefiles.  */
  730.  
  731.   default_file = enter_file (".DEFAULT");
  732.  
  733.   read_makefiles
  734.     = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
  735.  
  736.   /* Decode switches again, in case the variables were set by the makefile.  */
  737.   decode_env_switches ("MAKEFLAGS", 9);
  738.   decode_env_switches ("MFLAGS", 6);
  739.  
  740.   /* Set up MAKEFLAGS and MFLAGS again, so they will be right.  */
  741.  
  742.   define_makeflags (1, 0);
  743.  
  744.   ignore_errors_flag |= lookup_file (".IGNORE") != 0;
  745.  
  746.   silent_flag |= lookup_file (".SILENT") != 0;
  747.  
  748.   /* Make each `struct dep' point at the
  749.      `struct file' for the file depended on.  */
  750.  
  751.   snap_deps ();
  752.  
  753.   /* Install the default implicit rules.
  754.      This used to be done before reading the makefiles.
  755.      But in that case, built-in pattern rules were in the chain
  756.      before user-defined ones, so they matched first.  */
  757.  
  758.   install_default_implicit_rules ();
  759.  
  760.   /* Convert old-style suffix rules to pattern rules.  */
  761.  
  762.   convert_to_pattern ();
  763.  
  764.   /* Compute implicit rule limits.  */
  765.  
  766.   count_implicit_rule_limits ();
  767.  
  768.   /* Construct the listings of directories in VPATH lists.  */
  769.  
  770.   build_vpath_lists ();
  771.  
  772.   /* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
  773.      and as having been updated already, and files given with -W flags
  774.      as brand new (time-stamp of now).  */
  775.  
  776.   if (old_files != 0)
  777.     for (p = old_files->list; *p != 0; ++p)
  778.       {
  779.     f = enter_file (*p);
  780.     f->last_mtime = (time_t) 1;
  781.     f->updated = 1;
  782.     f->update_status = 0;
  783.     f->command_state = cs_finished;
  784.       }
  785.  
  786.   if (new_files != 0)
  787.     {
  788.       now = time ((time_t *) 0);
  789.       for (p = new_files->list; *p != 0; ++p)
  790.     {
  791.       f = enter_file (*p);
  792.       f->last_mtime = now;
  793.     }
  794.     }
  795.  
  796.   if (read_makefiles != 0)
  797.     {
  798.       /* Update any makefiles if necessary.  */
  799.  
  800.       time_t *makefile_mtimes = 0;
  801.       unsigned int mm_idx = 0;
  802.  
  803.       if (debug_flag)
  804.     puts ("Updating makefiles....");
  805.  
  806.       /* Remove any makefiles we don't want to try to update.
  807.      Also record the current modtimes so we can compare them later.  */
  808.       {
  809.     register struct dep *d, *last;
  810.     last = 0;
  811.     d = read_makefiles;
  812.     while (d != 0)
  813.       {
  814.         register struct file *f = d->file;
  815.         if (f->double_colon)
  816.           do
  817.         {
  818.           if (f->deps == 0 && f->cmds != 0)
  819.             {
  820.               /* This makefile is a :: target with commands, but
  821.              no dependencies.  So, it will always be remade.
  822.              This might well cause an infinite loop, so don't
  823.              try to remake it.  (This will only happen if
  824.              your makefiles are written exceptionally
  825.              stupidly; but if you work for Athena, that's how
  826.              you write your makefiles.)  */
  827.  
  828.               if (debug_flag)
  829.             printf ("Makefile `%s' might loop; not remaking it.\n",
  830.                 f->name);
  831.  
  832.               if (last == 0)
  833.             read_makefiles = d->next;
  834.               else
  835.             last->next = d->next;
  836.  
  837.               /* Free the storage.  */
  838.               free ((char *) d);
  839.  
  840.               d = last == 0 ? 0 : last->next;
  841.  
  842.               break;
  843.             }
  844.           f = f->prev;
  845.         }
  846.           while (f != NULL);
  847.         if (f == NULL || !f->double_colon)
  848.           {
  849.         if (makefile_mtimes == 0)
  850.           makefile_mtimes = (time_t *) xmalloc (sizeof (time_t));
  851.         else
  852.           makefile_mtimes = (time_t *)
  853.             xrealloc ((char *) makefile_mtimes,
  854.                   (mm_idx + 1) * sizeof (time_t));
  855.         makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
  856.         last = d;
  857.         d = d->next;
  858.           }
  859.       }
  860.       }    
  861.  
  862.       /* Set up `MAKEFLAGS' specially while remaking makefiles.  */
  863.       define_makeflags (1, 1);
  864.  
  865.       switch (update_goal_chain (read_makefiles, 1))
  866.     {
  867.     default:
  868.       abort ();
  869.       
  870.     case -1:
  871.       /* Did nothing.  */
  872.       break;
  873.       
  874.     case 1:
  875.       /* Failed to update.  Figure out if we care.  */
  876.       {
  877.         /* Nonzero if any makefile was successfully remade.  */
  878.         int any_remade = 0;
  879.         /* Nonzero if any makefile we care about failed
  880.            in updating or could not be found at all.  */
  881.         int any_failed = 0;
  882.         register unsigned int i;
  883.  
  884.         for (i = 0; read_makefiles != 0; ++i)
  885.           {
  886.         struct dep *d = read_makefiles;
  887.         read_makefiles = d->next;
  888.         if (d->file->updated)
  889.           {
  890.             /* This makefile was updated.  */
  891.             if (d->file->update_status == 0)
  892.               {
  893.             /* It was successfully updated.  */
  894.             any_remade |= (file_mtime_no_search (d->file)
  895.                        != makefile_mtimes[i]);
  896.               }
  897.             else if (d->changed != 1)
  898.               {
  899.             time_t mtime;
  900.             /* The update failed and this makefile was not
  901.                from the MAKEFILES variable, so we care.  */
  902.             error ("Failed to remake makefile `%s'.",
  903.                    d->file->name);
  904.             mtime = file_mtime_no_search (d->file);
  905.             any_remade |= (mtime != (time_t) -1
  906.                        && mtime != makefile_mtimes[i]);
  907.               }
  908.           }
  909.         else
  910.           /* This makefile was not found at all.  */
  911.           switch (d->changed)
  912.             {
  913.             case 0:
  914.               /* A normal makefile.  We must die later.  */
  915.               error ("Makefile `%s' was not found", dep_name (d));
  916.               any_failed = 1;
  917.               break;
  918.             case 1:
  919.               /* A makefile from the MAKEFILES variable.
  920.              We don't care.  */
  921.               break;
  922.             case 2:
  923.               /* An included makefile.  We don't need
  924.              to die, but we do want to complain.  */
  925.               error ("Included makefile `%s' was not found.",
  926.                  dep_name (d));
  927.               break;
  928.             }
  929.  
  930.         free ((char *) d);
  931.           }
  932.  
  933.         if (any_remade)
  934.           goto re_exec;
  935.         else if (any_failed)
  936.           die (1);
  937.         else
  938.           break;
  939.       }
  940.  
  941.     case 0:
  942.     re_exec:;
  943.       /* Updated successfully.  Re-exec ourselves.  */
  944.       if (print_directory_flag)
  945.         log_working_directory (0);
  946.       if (debug_flag)
  947.         puts ("Re-execing myself....");
  948.       if (makefiles != 0)
  949.         {
  950.           /* These names might have changed.  */
  951.           register unsigned int i, j = 0;
  952.           for (i = 1; i < argc; ++i)
  953.         if (!strcmp (argv[i], "-f"))
  954.           {
  955.             char *p = &argv[i][2];
  956.             if (*p == '\0')
  957.               argv[++i] = makefiles->list[j];
  958.             else
  959.               argv[i] = concat ("-f", makefiles->list[j], "");
  960.             ++j;
  961.           }
  962.         }
  963.       if (directories != 0 && directories->idx > 0)
  964.         {
  965.           char bad;
  966.           if (directory_before_chdir != 0)
  967.         {
  968.           if (chdir (directory_before_chdir) < 0)
  969.             {
  970.               perror_with_name ("chdir", "");
  971.               bad = 1;
  972.             }
  973.           else
  974.             bad = 0;
  975.         }
  976.           else
  977.         bad = 1;
  978.           if (bad)
  979.         fatal ("Couldn't change back to original directory.");
  980.         }
  981.       fflush (stdout);
  982.       fflush (stderr);
  983.       for (p = environ; *p != 0; ++p)
  984.         if (!strncmp (*p, "MAKELEVEL=", 10))
  985.           {
  986.         /* The SGI compiler apparently can't understand
  987.            the concept of storing the result of a function
  988.            in something other than a local variable.  */
  989.         char *sgi_loses;
  990.         sgi_loses = (char *) alloca (40);
  991.         *p = sgi_loses;
  992.         sprintf (*p, "MAKELEVEL=%u", makelevel);
  993.         break;
  994.           }
  995.       exec_command (argv, environ);
  996.       /* NOTREACHED */
  997.     }
  998.     }
  999.  
  1000.   /* Set up `MAKEFLAGS' again for the normal targets.  */
  1001.   define_makeflags (1, 0);
  1002.  
  1003.   {
  1004.     int status;
  1005.  
  1006.     /* If there were no command-line goals, use the default.  */
  1007.     if (goals == 0)
  1008.       {
  1009.     if (default_goal_file != 0)
  1010.       {
  1011.         goals = (struct dep *) xmalloc (sizeof (struct dep));
  1012.         goals->next = 0;
  1013.         goals->name = 0;
  1014.         goals->file = default_goal_file;
  1015.       }
  1016.       }
  1017.     else
  1018.       lastgoal->next = 0;
  1019.  
  1020.     if (goals != 0)
  1021.       {
  1022.     /* Update the goals.  */
  1023.  
  1024.     if (debug_flag)
  1025.       puts ("Updating goal targets....");
  1026.  
  1027.     switch (update_goal_chain (goals, 0))
  1028.       {
  1029.       case -1:
  1030.         /* Nothing happened.  */
  1031.       case 0:
  1032.         /* Updated successfully.  */
  1033.         status = 0;
  1034.         break;
  1035.       case 1:
  1036.         /* Updating failed.  */
  1037.         status = 1;
  1038.         break;
  1039.       default:
  1040.         abort ();
  1041.       }
  1042.       }
  1043.     else
  1044.       {
  1045.     if (read_makefiles == 0)
  1046.       fatal ("No targets specified and no makefile found");
  1047.     else
  1048.       fatal ("No targets");
  1049.       }
  1050.  
  1051.     /* Exit.  */
  1052.     die (status);
  1053.   }
  1054.  
  1055.   return 0;
  1056. }
  1057.  
  1058. /* Parsing of arguments, decoding of switches.  */
  1059.  
  1060. static char options[sizeof (switches) / sizeof (switches[0]) * 3];
  1061. static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
  1062.                   (sizeof (long_option_aliases) /
  1063.                    sizeof (long_option_aliases[0]))];
  1064.  
  1065. /* Fill in the string and vector for getopt.  */
  1066. static void
  1067. init_switches ()
  1068. {
  1069.   register char *p;
  1070.   register int c;
  1071.   register unsigned int i;
  1072.  
  1073.   if (options[0] != '\0')
  1074.     /* Already done.  */
  1075.     return;
  1076.  
  1077.   p = options;
  1078.   for (i = 0; switches[i].c != '\0'; ++i)
  1079.     {
  1080.       long_options[i].name = (switches[i].long_name == 0 ? "" :
  1081.                   switches[i].long_name);
  1082.       long_options[i].flag = 0;
  1083.       long_options[i].val = switches[i].c;
  1084.       if (isalnum (switches[i].c))
  1085.     *p++ = switches[i].c;
  1086.       switch (switches[i].type)
  1087.     {
  1088.     case flag:
  1089.     case flag_off:
  1090.     case ignore:
  1091.       long_options[i].has_arg = no_argument;
  1092.       break;
  1093.  
  1094.     case string:
  1095.     case positive_int:
  1096.     case floating:
  1097.       if (isalnum (switches[i].c))
  1098.         *p++ = ':';
  1099.       if (switches[i].noarg_value != 0)
  1100.         {
  1101.           if (isalnum (switches[i].c))
  1102.         *p++ = ':';
  1103.           long_options[i].has_arg = optional_argument;
  1104.         }
  1105.       else
  1106.         long_options[i].has_arg = required_argument;
  1107.       break;
  1108.     }
  1109.     }
  1110.   *p = '\0';
  1111.   for (c = 0; c < (sizeof (long_option_aliases) /
  1112.            sizeof (long_option_aliases[0]));
  1113.        ++c)
  1114.     long_options[i++] = long_option_aliases[c];
  1115.   long_options[i].name = 0;
  1116. }
  1117.  
  1118. /* Decode switches from ARGC and ARGV.
  1119.    They came from the environment if ENV is nonzero.  */
  1120.  
  1121. static void
  1122. decode_switches (argc, argv, env)
  1123.      int argc;
  1124.      char **argv;
  1125.      int env;
  1126. {
  1127.   int bad = 0;
  1128.   register const struct command_switch *cs;
  1129.   register struct stringlist *sl;
  1130.   register int c;
  1131.  
  1132.   if (!env)
  1133.     {
  1134.       other_args = (struct stringlist *) xmalloc (sizeof (struct stringlist));
  1135.       other_args->max = argc + 1;
  1136.       other_args->list = (char **) xmalloc ((argc + 1) * sizeof (char *));
  1137.       other_args->idx = 1;
  1138.       other_args->list[0] = savestring (argv[0], strlen (argv[0]));
  1139.     }
  1140.  
  1141.   /* getopt does most of the parsing for us.
  1142.      First, get its vectors set up.  */
  1143.  
  1144.   init_switches ();
  1145.  
  1146.   /* Let getopt produce error messages for the command line,
  1147.      but not for options from the environment.  */
  1148.   opterr = !env;
  1149.   /* Reset getopt's state.  */
  1150.   optind = 0;
  1151.  
  1152.   while ((c = getopt_long (argc, argv,
  1153.                options, long_options, (int *) 0)) != EOF)
  1154.     {
  1155.       if (c == '?')
  1156.     /* Bad option.  We will print a usage message and die later.
  1157.        But continue to parse the other options so the user can
  1158.        see all he did wrong.  */
  1159.     bad = 1;
  1160.       else
  1161.     for (cs = switches; cs->c != '\0'; ++cs)
  1162.       if (cs->c == c)
  1163.         {
  1164.           /* Whether or not we will actually do anything with
  1165.          this switch.  We test this individually inside the
  1166.          switch below rather than just once outside it, so that
  1167.          options which are to be ignored still consume args.  */
  1168.           int doit = !env || cs->env;
  1169.  
  1170.           switch (cs->type)
  1171.         {
  1172.         default:
  1173.           abort ();
  1174.  
  1175.         case ignore:
  1176.           break;
  1177.  
  1178.         case flag:
  1179.         case flag_off:
  1180.           if (doit)
  1181.             *(int *) cs->value_ptr = cs->type == flag;
  1182.           break;
  1183.  
  1184.         case string:
  1185.           if (!doit)
  1186.             break;
  1187.  
  1188.           if (optarg == 0)
  1189.             optarg = cs->noarg_value;
  1190.  
  1191.           sl = *(struct stringlist **) cs->value_ptr;
  1192.           if (sl == 0)
  1193.             {
  1194.               sl = (struct stringlist *)
  1195.             xmalloc (sizeof (struct stringlist));
  1196.               sl->max = 5;
  1197.               sl->idx = 0;
  1198.               sl->list = (char **) xmalloc (5 * sizeof (char *));
  1199.               *(struct stringlist **) cs->value_ptr = sl;
  1200.             }
  1201.           else if (sl->idx == sl->max - 1)
  1202.             {
  1203.               sl->max += 5;
  1204.               sl->list = (char **)
  1205.             xrealloc ((char *) sl->list,
  1206.                   sl->max * sizeof (char *));
  1207.             }
  1208.           sl->list[sl->idx++] = savestring (optarg, strlen (optarg));
  1209.           sl->list[sl->idx] = 0;
  1210.           break;
  1211.  
  1212.         case positive_int:
  1213.           if (optarg == 0 && argc > optind
  1214.               && isdigit (argv[optind][0]))
  1215.             optarg = argv[optind++];
  1216.  
  1217.           if (!doit)
  1218.             break;
  1219.  
  1220.           if (optarg != 0)
  1221.             {
  1222.               int i = atoi (optarg);
  1223.               if (i < 1)
  1224.             {
  1225.               if (doit)
  1226.                 error ("the `-%c' option requires a \
  1227. positive integral argument",
  1228.                    cs->c);
  1229.               bad = 1;
  1230.             }
  1231.               else
  1232.             *(unsigned int *) cs->value_ptr = i;
  1233.             }
  1234.           else
  1235.             *(unsigned int *) cs->value_ptr
  1236.               = *(unsigned int *) cs->noarg_value;
  1237.           break;
  1238.  
  1239.         case floating:
  1240.           if (optarg == 0 && optind < argc
  1241.               && (isdigit (argv[optind][0]) || argv[optind][0] == '.'))
  1242.             optarg = argv[optind++];
  1243.  
  1244.           if (doit)
  1245.             *(double *) cs->value_ptr            
  1246.               = (optarg != 0 ? atof (optarg)
  1247.              : *(double *) cs->noarg_value);
  1248.  
  1249.           break;
  1250.         }
  1251.         
  1252.           /* We've found the switch.  Stop looking.  */
  1253.           break;
  1254.         }
  1255.     }
  1256.  
  1257.   if (!env)
  1258.     {
  1259.       /* Collect the remaining args in the `other_args' string list.  */
  1260.  
  1261.       while (optind < argc)
  1262.     {
  1263.       char *arg = argv[optind++];
  1264.       if (arg[0] != '-' || arg[1] != '\0')
  1265.         other_args->list[other_args->idx++] = arg;
  1266.     }
  1267.       other_args->list[other_args->idx] = 0;
  1268.     }
  1269.  
  1270.   if (!env && (bad || print_usage_flag))
  1271.     {
  1272.       /* Print a nice usage message.  */
  1273.  
  1274.       if (print_version_flag)
  1275.     print_version ();
  1276.  
  1277.       fprintf (stderr, "Usage: %s [options] [target] ...\n", program);
  1278.  
  1279.       fputs ("Options:\n", stderr);
  1280.       for (cs = switches; cs->c != '\0'; ++cs)
  1281.     {
  1282.       char buf[1024], arg[50], *p;
  1283.  
  1284.       if (cs->description[0] == '-')
  1285.         continue;
  1286.  
  1287.       switch (long_options[cs - switches].has_arg)
  1288.         {
  1289.         case no_argument:
  1290.           arg[0] = '\0';
  1291.           break;
  1292.         case required_argument:
  1293.           sprintf (arg, " %s", cs->argdesc);
  1294.           break;
  1295.         case optional_argument:
  1296.           sprintf (arg, " [%s]", cs->argdesc);
  1297.           break;
  1298.         }
  1299.  
  1300.       p = buf;
  1301.  
  1302.       if (isalnum (cs->c))
  1303.         {
  1304.           sprintf (buf, "  -%c%s", cs->c, arg);
  1305.           p += strlen (p);
  1306.         }
  1307.       if (cs->long_name != 0)
  1308.         {
  1309.           unsigned int i;
  1310.           sprintf (p, "%s--%s%s",
  1311.                !isalnum (cs->c) ? "  " : ", ",
  1312.                cs->long_name, arg);
  1313.           p += strlen (p);
  1314.           for (i = 0; i < (sizeof (long_option_aliases) /
  1315.                    sizeof (long_option_aliases[0]));
  1316.            ++i)
  1317.         if (long_option_aliases[i].val == cs->c)
  1318.           {
  1319.             sprintf (p, ", --%s%s", long_option_aliases[i].name, arg);
  1320.             p += strlen (p);
  1321.           }
  1322.         }
  1323.       {
  1324.         const struct command_switch *ncs = cs;
  1325.         while ((++ncs)->c != '\0')
  1326.           if (ncs->description[0] == '-' &&
  1327.           ncs->description[1] == cs->c)
  1328.         {
  1329.           /* This is another switch that does the same
  1330.              one as the one we are processing.  We want
  1331.              to list them all together on one line.  */
  1332.           sprintf (p, ", -%c%s", ncs->c, arg);
  1333.           p += strlen (p);
  1334.           if (ncs->long_name != 0)
  1335.             {
  1336.               sprintf (p, ", --%s%s", ncs->long_name, arg);
  1337.               p += strlen (p);
  1338.             }
  1339.         }
  1340.       }
  1341.  
  1342.       if (p - buf > DESCRIPTION_COLUMN - 2)
  1343.         /* The list of option names is too long to fit on the same
  1344.            line with the description, leaving at least two spaces.
  1345.            Print it on its own line instead.  */
  1346.         {
  1347.           fprintf (stderr, "%s\n", buf);
  1348.           buf[0] = '\0';
  1349.         }
  1350.  
  1351.       fprintf (stderr, "%*s%s.\n",
  1352.            - DESCRIPTION_COLUMN,
  1353.            buf, cs->description);
  1354.     }
  1355.  
  1356.       die (bad);
  1357.     }
  1358. }
  1359.  
  1360. /* Decode switches from environment variable ENVAR (which is LEN chars long).
  1361.    We do this by chopping the value into a vector of words, prepending a
  1362.    dash to the first word if it lacks one, and passing the vector to
  1363.    decode_switches.  */
  1364.  
  1365. static void
  1366. decode_env_switches (envar, len)
  1367.      char *envar;
  1368.      unsigned int len;
  1369. {
  1370.   char *varref = (char *) alloca (2 + len + 2);
  1371.   char *value, *args;
  1372.   int argc;
  1373.   char **argv;
  1374.  
  1375.   /* Get the variable's value.  */
  1376.   varref[0] = '$';
  1377.   varref[1] = '(';
  1378.   bcopy (envar, &varref[2], len);
  1379.   varref[2 + len] = ')';
  1380.   varref[2 + len + 1] = '\0';
  1381.   value = variable_expand (varref);
  1382.  
  1383.   /* Skip whitespace, and check for an empty value.  */
  1384.   value = next_token (value);
  1385.   len = strlen (value);
  1386.   if (len == 0)
  1387.     return;
  1388.  
  1389.   /* Make a copy of the value in ARGS, where we will munge it.
  1390.      If it does not begin with a dash, prepend one.  */
  1391.   args = (char *) alloca (1 + len + 2);
  1392.   if (value[0] != '-')
  1393.     args[0] = '-';
  1394.   bcopy (value, value[0] == '-' ? args : &args[1], len + 1);
  1395.   /* Write an extra null terminator so our loop below will
  1396.      never be in danger of looking past the end of the string.  */
  1397.   args[(value[0] == '-' ? 0 : 1) + len + 1] = '\0';
  1398.  
  1399.   /* Allocate a vector that is definitely big enough.  */
  1400.   argv = (char **) alloca ((1 + len + 1) * sizeof (char *));
  1401.  
  1402.   /* getopt will look at the arguments starting at ARGV[1].
  1403.      Prepend a spacer word.  */
  1404.   argv[0] = 0;
  1405.   argc = 1;
  1406.   do
  1407.     {
  1408.       argv[argc++] = args;
  1409.       args = end_of_token (args);
  1410.       *args++ = '\0';
  1411.     } while (*args != '\0');
  1412.   argv[argc] = 0;
  1413.  
  1414.   /* Parse those words.  */
  1415.   decode_switches (argc, argv, 1);
  1416. }
  1417.  
  1418. /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
  1419.    command switches.  Include options with args if ALL is nonzero.
  1420.    Don't include options with the `no_makefile' flag set if MAKEFILE.  */
  1421.  
  1422. static void
  1423. define_makeflags (all, makefile)
  1424.      int all, makefile;
  1425. {
  1426.   register const struct command_switch *cs;
  1427.   char *flagstring;
  1428.  
  1429.   /* We will construct a linked list of `struct flag's describing
  1430.      all the flags which need to go in MAKEFLAGS.  Then, once we
  1431.      know how many there are and their lengths, we can put them all
  1432.      together in a string.  */
  1433.  
  1434.   struct flag
  1435.     {
  1436.       struct flag *next;
  1437.       const struct command_switch *cs;
  1438.       char *arg;
  1439.       unsigned int arglen;
  1440.     };
  1441.   struct flag *flags = 0;
  1442.   unsigned int flagslen = 0;
  1443. #define    ADD_FLAG(ARG, LEN) \
  1444.   do {                                          \
  1445.     struct flag *new = (struct flag *) alloca (sizeof (struct flag));          \
  1446.     new->cs = cs;                                  \
  1447.     new->arg = (ARG);                                  \
  1448.     new->arglen = (LEN);                              \
  1449.     new->next = flags;                                  \
  1450.     flags = new;                                  \
  1451.     if (new->arg == 0)                                  \
  1452.       ++flagslen;        /* Just a single flag letter.  */          \
  1453.     else                                      \
  1454.       flagslen += 1 + 1 + 1 + 1 + new->arglen; /* " -x foo" */              \
  1455.     if (!isalnum (cs->c))                              \
  1456.       /* This switch has no single-letter version, so we use the long.  */    \
  1457.       flagslen += 2 + strlen (cs->long_name);                      \
  1458.   } while (0)
  1459.  
  1460.   for (cs = switches; cs->c != '\0'; ++cs)
  1461.     if (cs->toenv && (!makefile || !cs->no_makefile))
  1462.       switch (cs->type)
  1463.     {
  1464.     default:
  1465.       abort ();
  1466.  
  1467.     case ignore:
  1468.       break;
  1469.  
  1470.     case flag:
  1471.     case flag_off:
  1472.       if (!*(int *) cs->value_ptr == (cs->type == flag_off)
  1473.           && (cs->default_value == 0
  1474.           || *(int *) cs->value_ptr != *(int *) cs->default_value))
  1475.         ADD_FLAG (0, 0);
  1476.       break;
  1477.  
  1478.     case positive_int:
  1479.       if (all)
  1480.         {
  1481.           if ((cs->default_value != 0
  1482.            && (*(unsigned int *) cs->value_ptr
  1483.                == *(unsigned int *) cs->default_value)))
  1484.         break;
  1485.           else if (cs->noarg_value != 0
  1486.                && (*(unsigned int *) cs->value_ptr ==
  1487.                *(unsigned int *) cs->noarg_value))
  1488.         ADD_FLAG ("", 0); /* Optional value omitted; see below.  */
  1489.           else if (cs->c == 'j')
  1490.         /* Special case for `-j'.  */
  1491.         ADD_FLAG ("1", 1);
  1492.           else
  1493.         {
  1494.           char *buf = (char *) alloca (30);
  1495.           sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
  1496.           ADD_FLAG (buf, strlen (buf));
  1497.         }
  1498.         }
  1499.       break;
  1500.  
  1501.     case floating:
  1502.       if (all)
  1503.         {
  1504.           if (cs->default_value != 0
  1505.           && (*(double *) cs->value_ptr
  1506.               == *(double *) cs->default_value))
  1507.         break;
  1508.           else if (cs->noarg_value != 0
  1509.                && (*(double *) cs->value_ptr
  1510.                == *(double *) cs->noarg_value))
  1511.         ADD_FLAG ("", 0); /* Optional value omitted; see below.  */
  1512.           else
  1513.         {
  1514.           char *buf = (char *) alloca (100);
  1515.           sprintf (buf, "%g", *(double *) cs->value_ptr);
  1516.           ADD_FLAG (buf, strlen (buf));
  1517.         }
  1518.         }
  1519.       break;
  1520.  
  1521.     case string:
  1522.       if (all)
  1523.         {
  1524.           struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
  1525.           if (sl != 0)
  1526.         {
  1527.           /* Add the elements in reverse order, because
  1528.              all the flags get reversed below; and the order
  1529.              matters for some switches (like -I).  */
  1530.           register unsigned int i = sl->idx;
  1531.           while (i-- > 0)
  1532.             ADD_FLAG (sl->list[i], strlen (sl->list[i]));
  1533.         }
  1534.         }
  1535.       break;
  1536.     }
  1537.  
  1538. #undef    ADD_FLAG
  1539.  
  1540.   if (flags == 0)
  1541.     /* No flags.  Use a string of two nulls so [1] works below.  */
  1542.     flagstring = "\0";
  1543.   else
  1544.     {
  1545.       /* Construct the value in FLAGSTRING.
  1546.      We allocate enough space for a preceding dash and trailing null.  */
  1547.       register char *p;
  1548.       flagstring = (char *) alloca (1 + flagslen + 1);
  1549.       p = flagstring;
  1550.       *p++ = '-';
  1551.       do
  1552.     {
  1553.       /* Add the flag letter or name to the string.  */
  1554.       if (!isalnum (flags->cs->c))
  1555.         {
  1556.           *p++ = '-';
  1557.           strcpy (p, flags->cs->long_name);
  1558.           p += strlen (p);
  1559.         }
  1560.       else
  1561.         *p++ = flags->cs->c;
  1562.       if (flags->arg != 0)
  1563.         {
  1564.           /* A flag that takes an optional argument which in this case
  1565.          is omitted is specified by ARG being "" and ARGLEN being 0.
  1566.          We must distinguish because a following flag appended without
  1567.          an intervening " -" is considered the arg for the first.  */
  1568.           if (flags->arglen > 0)
  1569.         {
  1570.           /* Add its argument too.  */
  1571.           *p++ = !isalnum (flags->cs->c) ? '=' : ' ';
  1572.           bcopy (flags->arg, p, flags->arglen);
  1573.           p += flags->arglen;
  1574.         }
  1575.           /* Write a following space and dash, for the next flag.  */
  1576.           *p++ = ' ';
  1577.           *p++ = '-';
  1578.         }
  1579.       else if (!isalnum (flags->cs->c))
  1580.         {
  1581.           /* Long options must each go in their own word,
  1582.          so we write the following space and dash.  */
  1583.           *p++ = ' ';
  1584.           *p++ = '-';
  1585.         }
  1586.       flags = flags->next;
  1587.     } while (flags != 0);
  1588.  
  1589.       if (p[-1] == '-')
  1590.     /* Kill the final space and dash.  */
  1591.     p -= 2;
  1592.  
  1593.       /* Terminate the string.  */
  1594.       *p = '\0';
  1595.     }
  1596.  
  1597.   (void) define_variable ("MAKEFLAGS", 9,
  1598.               /* On Sun, the value of MFLAGS starts with a `-' but
  1599.                  the value of MAKEFLAGS lacks the `-'.
  1600.                  Be compatible with this unless FLAGSTRING starts
  1601.                  with a long option `--foo', since removing the
  1602.                  first dash would result in the bogus `-foo'.  */
  1603.               flagstring[1] == '-' ? flagstring : &flagstring[1],
  1604.               o_env, 0);
  1605.   (void) define_variable ("MFLAGS", 6, flagstring, o_env, 0);
  1606. }
  1607.  
  1608. /* Print version information.  */
  1609.  
  1610. static void
  1611. print_version ()
  1612. {
  1613.   static int printed_version = 0;
  1614.  
  1615.   extern char *remote_description;
  1616.   char *precede = print_data_base_flag ? "# " : "";
  1617.  
  1618.   if (printed_version)
  1619.     /* Do it only once.  */
  1620.     return;
  1621.  
  1622.   printf ("%sGNU Make version %s", precede, version_string);
  1623.   if (remote_description != 0 && *remote_description != '\0')
  1624.     printf ("-%s", remote_description);
  1625.  
  1626.   printf (", by Richard Stallman and Roland McGrath.\n\
  1627. %sCopyright (C) 1988, 89, 90, 91, 92, 93 Free Software Foundation, Inc.\n\
  1628. %sThis is free software; see the source for copying conditions.\n\
  1629. %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
  1630. %sPARTICULAR PURPOSE.\n\n", precede, precede, precede, precede);
  1631.  
  1632.   printed_version = 1;
  1633.  
  1634.   /* Flush stdout so the user doesn't have to wait to see the
  1635.      version information while things are thought about.  */
  1636.   fflush (stdout);
  1637. }
  1638.  
  1639. /* Print a bunch of information about this and that.  */
  1640.  
  1641. static void
  1642. print_data_base ()
  1643. {
  1644.   extern char *ctime ();
  1645.   time_t when;
  1646.  
  1647.   when = time ((time_t *) 0);
  1648.   printf ("\n# Make data base, printed on %s", ctime (&when));
  1649.  
  1650.   print_variable_data_base ();
  1651.   print_dir_data_base ();
  1652.   print_rule_data_base ();
  1653.   print_file_data_base ();
  1654.   print_vpath_data_base ();
  1655.  
  1656.   when = time ((time_t *) 0);
  1657.   printf ("\n# Finished Make data base on %s\n", ctime (&when));
  1658. }
  1659.  
  1660. /* Exit with STATUS, cleaning up as necessary.  */
  1661.  
  1662. void
  1663. die (status)
  1664.      int status;
  1665. {
  1666.   static char dying = 0;
  1667.  
  1668.   if (!dying)
  1669.     {
  1670.       int err;
  1671.  
  1672.       dying = 1;
  1673.  
  1674.       if (print_version_flag)
  1675.     print_version ();
  1676.  
  1677.       /* Wait for children to die.  */
  1678.       for (err = status != 0; job_slots_used > 0; err = 0)
  1679.     reap_children (1, err);
  1680.  
  1681.       /* Remove the intermediate files.  */
  1682.       remove_intermediates (0);
  1683.  
  1684.       if (print_data_base_flag)
  1685.     print_data_base ();
  1686.  
  1687.       if (print_directory_flag)
  1688.     log_working_directory (0);
  1689.     }
  1690.  
  1691.   exit (status);
  1692. }
  1693.  
  1694. /* Write a message indicating that we've just entered or
  1695.    left (according to ENTERING) the current directory.  */
  1696.  
  1697. static void
  1698. log_working_directory (entering)
  1699.      int entering;
  1700. {
  1701.   static int entered = 0;
  1702.   char *message = entering ? "Entering" : "Leaving";
  1703.  
  1704.   if (entering)
  1705.     entered = 1;
  1706.   else if (!entered)
  1707.     /* Don't print the leaving message if we
  1708.        haven't printed the entering message.  */
  1709.     return;
  1710.  
  1711.   if (print_data_base_flag)
  1712.     fputs ("# ", stdout);
  1713.  
  1714.   if (makelevel == 0)
  1715.     printf ("%s: %s ", program, message);
  1716.   else
  1717.     printf ("%s[%u]: %s ", program, makelevel, message);
  1718.  
  1719.   if (starting_directory == 0)
  1720.     puts ("an unknown directory");
  1721.   else
  1722.     printf ("directory `%s'\n", starting_directory);
  1723. }
  1724.